<?php
/**
 * Created by PhpStorm.
 * User: TangYijun
 * Date: 2017/4/1
 * Time: 14:44
 */
namespace app\wechat\controller;
use app\models\Bases;
use think\Config;

class User extends Base{
    /**
     * @return \think\response\Json
     * 验证用户是否绑定过手机号
     */
    public function checkBand(){
        try{
            $openid = cookie('openid');
            $user =  new Bases('user');
            $user_info = $user->find("openid = '{$openid}'");
            $is_bind = Config::get('wechat.is_not_bind');
            if(!empty($user_info['openid']) && !empty($user_info['username'])){
                $is_bind = Config::get('wechat.is_bind');
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult(['is_bind' => $is_bind]));
    }

    /**
     * @return \think\response\Json
     * 验证该用户的绑定状态
     */
    public function checkUsername(){
        try{
            $post = input('param.');
            $result = self::_getUserInfo("username = '{$post['username']}'","uid,openid");
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($result));
    }

    /**
     * @return \think\response\View
     * 用户注册
     */
    public function register(){
        $openid = cookie('openid');
        $user_info = self::getUserInfo("openid = '{$openid}'","nickname");
        return view('register',['user_info'=>$user_info]);
    }

    /**
     * 保存用户资料
     */
    public function saveUserInfo(){
        $openid = cookie('openid');
        $user = new Bases('user');
        try{
            //验证验证码是否正确
            $post = input('param.');
            if(empty($post)){
                throw new \LogicException('参数错误',10028);
            }
            if(empty($openid)){
                throw new \LogicException('请重新登录',10025);
            }
            $user_info = self::getUserInfo("openid = '{$openid}'",'nickname,avatar');
            self::checkCode($post['code'],'chat_register',$post['username']);
            if($post['status'] == Config::get('wechat.is_new_user')){
                $data = [
                    'nickname' => $post['nickname'],
                    'password' => password_hash($post['password'],true),
                    'username' => $post['username'],
                    'is_remind' => 2,
                ];
                if($user->updateData($data,"openid = '{$openid}'") === false){
                    throw new \LogicException('操作失败',10025);
                }
            }
            if($post['status'] == Config::get('wechat.is_pc_user')){
                $user->startTrans();
                $delete_result = $user->deleteByWhere("openid = '{$openid}'");
                $update_result = $user->updateData(['openid' => $openid,'avatar'=>$user_info['avatar'],'is_remind'=>2],"username = '{$post['username']}'");
                if( $update_result === false || $delete_result === false){
                    $user->rollback();
                    throw new \LogicException('操作失败',10027);
                }
                $user->commit();
            }
            $user->_setInc("openid = '{$openid}'",'integral',50);
            $user_info = self::getUserInfo("openid = '{$openid}'",'uid');
            cookie('uid',$user_info['uid']); //此时也保存用户的id
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }


}